<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Validate;

/**
 * Description of App
 *
 * @author wyxnn
 */
class App extends Api {

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    public function _initialize() {
        parent::_initialize();
    }

    /**
     * 首页
     */
    public function home() {
        $sellerList = model('shop')
                ->where('status=1')
                ->field('id as sellerID,logo_image as sellerIcon,name as sellerName,abstract as sellerIntroduction')
                ->order('weigh desc')
                ->select();
        $goodsList = model('goods')
                ->where('status=1')
                ->field('id as goodsID,image as goodsIcon,name as goodsName,price as goodsPrice')
                ->order('weigh desc')
                ->select();
        $data['sellerList'] = $sellerList;
        $data['goodsList'] = $goodsList;
        $this->success('success', $data);
    }

    /**
     * 商家详情
     */
    public function sellerDetail() {
        $id = $this->request->post('sellerID');
        $row = model('shop')
                ->where('id', $id)
                ->field('logo_image as sellerLogo,main_image as sellerIcon,name as sellerName,abstract as sellerIntroduction')
                ->find();
        $this->success('success', $row);
    }

    /**
     * 商品列表
     */
    public function goodsSearch() {
        $shop_id = $this->request->post('sellerID', '');
        $goods_name = $this->request->post('goodsName', '');
        $where = [];
        if ($shop_id) {
            $where['shop_id'] = $shop_id;
        }
        if ($goods_name) {
            $where['name'] = ['like', '%' . $goods_name . '%'];
        }
        $row = model('goods')
                ->where($where)
                ->field('id as goodsID,image as goodsIcon,name as goodsName,price as goodsPrice')
                ->order('weigh desc')
                ->select();
        $this->success('success', $row);
    }

    /**
     * 商品详情
     */
    public function goodsDetail() {
        $id = $this->request->post('goodsID');
        $row = model('goods')->alias('a')
                ->join('shop b', 'b.id=a.shop_id')
                ->field('a.shop_id as sellerID,b.name as sellerName,a.image as goodsIcon,a.name as goodsName,a.price as goodsPrice,a.is_select as isTypical,a.spec as standard,a.content as goodsDetail,a.3d_image as goodsUrl')
                ->where('a.id', $id)
                ->order('a.weigh desc')
                ->select();
        $this->success('success', $row);
    }

}
